package com.google.code.java_csv_viewer;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.util.ArrayList;

public class CSVReader {
	private String filename;
	private Path file;
	private ArrayList<String[]> lines = new ArrayList<String[]>();
	
	CSVReader(String filename) {
		this.filename = filename;
		//FileSystems.getDefault().getPath(filename);
		file = Paths.get(filename);
		boolean existsAndReadble = Files.exists(file) && Files.isReadable(file);
		System.out.println("File " + filename + " exists: " + existsAndReadble);
		
		Charset charset = Charset.forName("UTF-8");
		try (BufferedReader reader = Files.newBufferedReader(file, charset)) {
		    String line = null;
		    while ((line = reader.readLine()) != null) {
		        lines.add(line.split(";"));
		    }
		} catch (IOException x) {
		    System.err.format("IOException: %s%n", x);
		}
	}
	
	public String[] getRow(int rowNr) {
		return lines.get(rowNr);
	}
	
	public String getData(int rowNr, int colNr) {
		return getRow(rowNr)[colNr];
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		CSVReader r = new CSVReader("/home/uffe/test.csv");
		System.out.println(r.getData(0,0));
	}

}
